﻿using AutoMapper;

using B.S.Traceability.Domain;
using B.S.Traceability.Error;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Read.API.Application.Commends;
using B.S.Traceability.Read.API.Dto;
using MediatR;
using System.Collections.Generic;

namespace B.S.Traceability.Read.API.Application.CommendHandlers
{
    public class ProductsShoopCommendHandlers : IRequestHandler<ProductsShoopCommends, APIPaging<List<ProductsShoopDtos>>>
    {

        private readonly IBaseRepository<Products> productsRepository;
        private readonly IBaseRepository<Shoops> shoopsRepository;
        private readonly IMapper mapper;

        public ProductsShoopCommendHandlers(IBaseRepository<Products> productsRepository, IBaseRepository<Shoops> shoopsRepository, IMapper mapper)
        {
            this.productsRepository = productsRepository;
            this.shoopsRepository = shoopsRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 获取生产信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIPaging<List<ProductsShoopDtos>>> Handle(ProductsShoopCommends request, CancellationToken cancellationToken)
        {
            APIPaging<List<ProductsShoopDtos>> result = new APIPaging<List<ProductsShoopDtos>>();


            result.Code = APIEnum.查询成功;
            result.Msg= "查询成功";

            var products = productsRepository.GetAllAsync().Result;
            var shoops= shoopsRepository.GetAllAsync().Result;

            var lists = from a in products
                        join b in shoops on a.ShoopId equals b.ShoopsId
                        where a.IsDel == false
                        select new ProductsShoopDtos
                        {
                            ProductsId = a.ProductsId,
                            ShoopId = a.ShoopId,
                            BatchId = a.BatchId,
                            Shelflife = a.Shelflife,
                            Productionnumber = a.Productionnumber,
                            Manufacturers = a.Manufacturers,
                            Productsimg = a.Productsimg,
                            CreateName = a.CreateName,
                            CreateTime = a.CreateTime,
                            UpdateName = a.UpdateName,
                            UpdateTime = a.UpdateTime,
                            IsDel = a.IsDel,
                            
                            ShoopsName = b.ShoopsName,
                        };

            if (!string.IsNullOrEmpty(request.ShoopsName)) {
             lists= lists.Where(x => x.ShoopsName.Contains(request.ShoopsName));
            }

            result.TotalCount = lists.Count();
            result.TotalPage = (int)(result.TotalCount * 1.0 / request.PageSize);
            lists = lists.OrderByDescending(p=>p.ProductsId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            var data = mapper.Map<List<ProductsShoopDtos>>(lists.ToList());
            result.data = data;
            return Task.FromResult(result);
        }
    }
}
